
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#include "miniutil/httpserver.h"
#include "miniutil/virtualhost.h"
#include "miniutil/httpclient.h"
#include "miniutil/srtti.h"
#include "miniutil/servlets.h"

#include "minitest/testcase.h"

#include <iostream>

using namespace miniutil;



class t_httpserver_test2 : public testcase
{

public: 
	t_httpserver_test2()
	{
		themain.tests.push_back( this );
	}

	virtual void test()
	{
		httpserver  * server;

		cout << "start httpserver test..." << endl;

		miniutil::srtti::srtti::_declare< miniutil::http::sysinfo_servlet > ("miniutil::http::sysinfo_servlet");

		const char *p_xmlconfig = ""
								"<server ip='0.0.0.0' port='3451' maxclient='10'>"
								"  <ipaccess deny='1'>"
								"    <ip_tables default_res='0' matched_res='1'>"
								"		<ip addr='127.0.0.2' res='1' />"
								"    </ip_tables>"
								"    <cnet_tables default_res='0' matched_res='1'>"
								"		<cnet addr='192.168.1.0' />"
								"    </cnet_tables>"
								"  </ipaccess>"
								"  <ipconcurrent maxcon='3' />"
								"  <virtualhosts>"
								"	<servlets path=''>"
								"		<module classname='miniutil::http::auth_servlet' dllname='srttidll.so' />"
								"		<module classname='miniutil::http::text_servlet' dllname='srttidll.so' />"
								"		<module classname='miniutil::http::doc_servlet' dllname='srttidll.so' />"
								"		<module classname='miniutil::http::control_servlet' dllname='srttidll.so' />"
								"		<module classname='miniutil::http::sysinfo_servlet' dllname='' />"
								"	</servlets>"
								"	<virtualhost host=''>"
								"	</virtualhost>"
								"	<virtualhost host='localhost'>"
								"		<servlet classname='miniutil::http::text_servlet'>"
								"			<matched type='suffix' uri='txt' flag='case' />"
								"			<config path='/home/web/vakaka/' type='text/plain' />"
								"		</servlet>"
								"		<servlet classname='miniutil::http::auth_servlet'>"
								"			<matched type='prefix' uri='/vtt' flag='nocase' />"
								"			<config realm='http_auth'>"
								"				<user name='user' pass='1234' />"
								"			</config>"
								"		</servlet>"
								"		<servlet classname='miniutil::http::control_servlet'>"
								"			<matched type='equal' uri='/control' flag='nocase' />"
								"			<config />"
								"		</servlet>"
								"		<servlet classname='miniutil::http::sysinfo_servlet'>"
								"			<matched type='equal' uri='/sysinfo' flag='nocase' />"
								"			<config />"
								"		</servlet>"
								"		<servlet classname='miniutil::http::doc_servlet'>"
								"			<matched type='prefix' uri='/' flag='nocase' />"
								"			<config path='d:/temp' type='text/plain' strip_prefix='0'>"
								"				<mime suffix='.css' type='text/css' />"
								"				<mime suffix='.txt' type='text/plain' />"
								"				<mime suffix='.html' type='text/html' />"
								"				<mime suffix='.htm' type='text/html' />"
								"				<mime suffix='.gif' type='image/gif' />"
								"			</config>"
								"		</servlet>"
								"	</virtualhost>"
								"  </virtualhosts>  "
								"</server>"
								" ";
	
		
		cout << "\nTest xmlconfig of virtual host ... \n";
		miniutil::tinyxml::TiXmlDocument configDoc;
		if ( configDoc.Parse( p_xmlconfig ) == NULL || configDoc.RootElement() == NULL )
		{
			cout << "\tFailed to parse xml config \n"<< p_xmlconfig << std::endl; 
			return;
		}
		else
			cout << "\tOK"<<std::endl;

		try {
			server = httpserver::create<httpserver>( configDoc.RootElement() );
			cout << " new server at 3451 ok";
		} catch(...)
		{
			cout << " new server at 3451 failed";
			return;
		}

		cout << "please use browser to check 'http://localhost:3451/check', and reload 2-3 times, and then test will go on!"<<endl;
		cout << "test server";


		server->run();
	}
} ;

testcase * thttpserver2 = new t_httpserver_test2();

